package jp.bsws.action.relation;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.JugyoinRelationBean;
import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.KeyValueObjectBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.JugyoinRelationDao;
import jp.bsws.model.JugyoinRelationModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class JugyoinRelationAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private JugyoinRelationBean jugyoinRelationBean;
	private List<KeyValueBean> sansyoHosyuShubetsuList;
	private List<KeyValueBean> sansyoJugyoinList;

	private Integer intSansyoJugyoinSeq;

	public void validateUpdateJugyoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//従業員関係情報が無ければ、一つ作る。
		if( this.jugyoinRelationBean == null){
			this.jugyoinRelationBean = new JugyoinRelationBean();
		}
		if(this.intSansyoJugyoinSeq == null){
			this.intSansyoJugyoinSeq = bSWSSession.getIntJugyoinSeq();
		}
		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();
		Connection con = DBUtils.getConnection();
		try{
			//従業員関連情報
			setJugyoinRelation(con);
		}finally{
			con.close();
		}
		return SUCCESS;
	}


	public String updateJugyoinRelation() throws Exception{

		//初期設定
		init();

		JugyoinRelationDao jugyoinRelationDao = JugyoinRelationDao.getInstance();

		Connection con = DBUtils.getConnection();
		try{
			// 従業員の情報全部削除
			jugyoinRelationDao.deleteJugyoinRelation(con, bSWSSession.getStrGroupCod(), intSansyoJugyoinSeq);
			// 従業員の情報全部登録
			jugyoinRelationDao.insertJugyoinRelationAll(con, bSWSSession, intSansyoJugyoinSeq, jugyoinRelationBean);

			con.commit();

			// 画面出力内容取得
			setJugyoinRelation(con);

		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{
		HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();
		JugyoinDao jugyoinDao = JugyoinDao.getInstance();

		// KeyValue情報取得
		this.sansyoHosyuShubetsuList = hosyuShubetsuDao.getHosyuShubetsuList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoJugyoinList = jugyoinDao.getJugyoinList(con, bSWSSession.getStrGroupCod(), null);
	}

	private void setJugyoinRelation(Connection con) throws Exception{
		JugyoinRelationDao jugyoinRelationDao = JugyoinRelationDao.getInstance();

		// 関連情報取得
		List<KeyValueObjectBean> tmpHSList = new ArrayList<KeyValueObjectBean>();
		for(int i = 0; i < this.sansyoHosyuShubetsuList.size(); i++){
			KeyValueBean kv = this.sansyoHosyuShubetsuList.get(i);
			String strSeq = kv.getKey();
			String strValue = kv.getValue();
			KeyValueObjectBean kvo = new KeyValueObjectBean();
			kvo.setKey(strSeq) ;
			kvo.setValue(strValue);

			List<JugyoinRelationModel> jugyoinRelationList = jugyoinRelationDao.getJugyoinRelationInfo(con, bSWSSession.getStrGroupCod(), intSansyoJugyoinSeq, BSWSConstants.KBN_HOSYU_SHUBETSU_WARIAI, strSeq);
			for(int k = 0; k < jugyoinRelationList.size(); k++){
				JugyoinRelationModel tempJugyoinRelationModel = jugyoinRelationList.get(k);
				switch (tempJugyoinRelationModel.getIntRelationKbn()) {
				case BSWSConstants.KBN_HOSYU_SHUBETSU_WARIAI:
					String strAtai = tempJugyoinRelationModel.getStrAtai();
					kvo.setAtai(strAtai) ;
					break;
				default:
					break;
				}
			}
			tmpHSList.add(kvo);
		}

		this.jugyoinRelationBean.setHosyuShubetsuList(tmpHSList);
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public JugyoinRelationBean getJugyoinRelationBean() {
		return jugyoinRelationBean;
	}

	public void setJugyoinRelationBean(JugyoinRelationBean jugyoinRelationBean) {
		this.jugyoinRelationBean = jugyoinRelationBean;
	}

	public List<KeyValueBean> getSansyoHosyuShubetsuList() {
		return sansyoHosyuShubetsuList;
	}

	public void setSansyoHosyuShubetsuList(
			List<KeyValueBean> sansyoHosyuShubetsuList) {
		this.sansyoHosyuShubetsuList = sansyoHosyuShubetsuList;
	}

	public List<KeyValueBean> getSansyoJugyoinList() {
		return sansyoJugyoinList;
	}

	public void setSansyoJugyoinList(List<KeyValueBean> sansyoJugyoinList) {
		this.sansyoJugyoinList = sansyoJugyoinList;
	}

	public Integer getIntSansyoJugyoinSeq() {
		return intSansyoJugyoinSeq;
	}

	public void setIntSansyoJugyoinSeq(Integer intSansyoJugyoinSeq) {
		this.intSansyoJugyoinSeq = intSansyoJugyoinSeq;
	}



}